﻿// Copyright (C) 2012 Jeff Tanner <jeff00seattle@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsAzure_StorageREST
{
    public class BlobContainer : BlobService
    {
        public string Name { get; set; }
        public Uri Url { get; set; }

        public BlobContainer(StorageEndpoint storageEndpoint)
            : base(storageEndpoint)
        {
        }

        public BlobContainer(StorageEndpoint storageEndpoint, string blobContainerName)
            : base(storageEndpoint)
        {
            this.Name = blobContainerName;
        }

        /// <summary>
        /// Sets the container permissions.
        /// </summary>
        /// <param name="isPublic">if set to <c>true</c> [is public].</param>
        /// <returns></returns>
        public BlobServiceResponse SetContainerPermissions(bool isPublic)
        {
            Dictionary<string, string> queryParams = new Dictionary<string, string>();
            Dictionary<string, string> functionHeaders = new Dictionary<string, string>();

            queryParams.Add("comp", "acl");
            functionHeaders.Add("x-ms-prop-publicaccess", isPublic.ToString().ToLower());

            base.InitializeRequest(this.Name, queryParams);
            base.SetMethod("PUT");
            base.SetFunctionHeaders(functionHeaders);

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Gets the blobs.
        /// </summary>
        /// <returns></returns>
        public BlobServiceResponse RetrieveBlobs()
        {
            Dictionary<string, string> queryParams = new Dictionary<string, string>();

            queryParams.Add("comp", "list");
            base.InitializeRequest(this.Name, queryParams);
            base.SetMethod("GET");

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Gets the containers.
        /// </summary>
        /// <returns></returns>
        public BlobServiceResponse RetrieveContainers()
        {
            Dictionary<string, string> queryParams = new Dictionary<string, string>();

            queryParams.Add("comp", "list");
            base.InitializeRequest(null, queryParams);
            base.SetMethod("GET");

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Creates a new container in the given storage account.
        /// </summary>
        /// <returns></returns>
        public BlobServiceResponse CreateContainer()
        {
            base.InitializeRequest(this.Name, null);
            base.SetMethod("PUT");

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Deletes the container and any blobs that it contains.
        /// </summary>
        /// <returns></returns>
        public BlobServiceResponse DeleteContainer()
        {
            base.InitializeRequest(this.Name, null);
            base.SetMethod("DELETE");

            return new BlobServiceResponse(base.DispatchRequest());
        }
    }
}
